#!/usr/bin/python
# -*- coding: UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
Data update provides download tasks

Authors: xiaojianghui
Date:    10/22/2020 11:01 AM
"""

import requests
import lxml.etree as etree
import shutil
import os


def handle_one():
    """
        download file
    """
    if os.listdir("./newexcels"):
        shutil.rmtree("./newexcels")
        os.mkdir("./newexcels")
    url = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/newexcels'
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            local_name = name.lower()
            if local_name.endswith(".xls") or local_name.endswith(".xlsx"):
                url = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/newexcels/" + name
                r = requests.get(url)
                print(r.status_code)
                with open("./newexcels/" + name, 'wb') as code:
                    code.write(r.content)
                    code.close()
            if os.path.exists("./newexcels/" + name):
                print("Successfully downloaded the cve manual data form:" + name)
            i += 2


def handle_two():
    """
        download file
    """
    if os.listdir("./mappingexcels"):
        shutil.rmtree("./mappingexcels")
        os.mkdir("./mappingexcels")
    url = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/mappingexcels'
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            if name.endswith(".xls") or name.endswith(".xlsx"):
                url = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/mappingexcels/" + name
                r = requests.get(url)
                print(r.status_code)
                with open("./mappingexcels/" + name, 'wb') as code:
                    code.write(r.content)
            if os.path.exists("./mappingexcels/" + name):
                print("Download the package name mapping table successfully:" + name)
            i += 2


def handle_three():
    """
        download file
    """
    if os.listdir("./import_excels"):
        shutil.rmtree("./import_excels")
        os.mkdir("./import_excels")
    url = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/import_excels'
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            if name.endswith(".xls") or name.endswith(".xlsx"):
                url = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/import_excels/" + name
                r = requests.get(url)
                print(r.status_code)
                with open("./import_excels/" + name, 'wb') as code:
                    code.write(r.content)
            if os.path.exists("./import_excels/" + name):
                print("Download the Package whitelist table successfully:" + name)
            i += 2


def handle_four():
    """
        download file
    """
    if os.listdir("./package_committer_excels"):
        shutil.rmtree("./package_committer_excels")
        os.mkdir("./package_committer_excels")
    url = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/package_committer_excels'
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            if name.endswith(".xls") or name.endswith(".xlsx"):
                url = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/package_committer_excels/" + name
                r = requests.get(url)
                print(r.status_code)
                with open("./package_committer_excels/" + name, 'wb') as code:
                    code.write(r.content)
            if os.path.exists("./package_committer_excels/" + name):
                print("Download the package_committer table successfully:" + name)
            i += 2


def download_spec_error_excels():
    """
        download spec_error_excels
    """
    if not os.path.exists('./spec_error_excels'):
        os.makedirs('./spec_error_excels')
    url = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/spec_error_excels'
    try:
        r = requests.get(url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            if name.endswith(".xls") or name.endswith(".xlsx"):
                url = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/spec_error_excels/" + name
                r = requests.get(url)
                print(r.status_code)
                with open("./spec_error_excels/" + name, 'wb') as code:
                    code.write(r.content)
            if os.path.exists("./spec_error_excels/" + name):
                print("Download the package_committer table successfully:" + name)
            i += 2


def download_yaml(file_path, file_url):
    """
    Download yaml data from gitee
    """
    try:
        r = requests.get(file_url)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    with open(file_path, "wb") as code:
        code.write(r.content)


def download_excel(file_dir):
    """
        download excel
    """
    local_dir = "./" + file_dir
    gitee_dir = 'https://gitee.com/openeuler/cve-manager/tree/master/cve-py/' + file_dir
    urlx = "https://gitee.com/openeuler/cve-manager/raw/master/cve-py/%s/" % (file_dir)
    if os.listdir(local_dir):
        shutil.rmtree(local_dir)
        os.mkdir(local_dir)
    try:
        r = requests.get(gitee_dir)
    except requests.exceptions.ConnectionError:
        print('ConnectionError')
        return
    except requests.exceptions.ChunkedEncodingError:
        print('ChunkedEncodingError')
        return
    html = etree.HTML(r.content)
    if len(html):
        i = 3
        while True:
            try:
                name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
            except IndexError:
                break
            if name.endswith(".xls") or name.endswith(".xlsx"):
                url = urlx + name
                r = requests.get(url)
                print(r.status_code)
                with open(local_dir + "/" + name, 'wb') as code:
                    code.write(r.content)
            if os.path.exists(local_dir + "/" + name):
                print("Download the excel successfully:" + name)
            i += 2
